﻿using System;
using System.Windows;
using System.Windows.Media;
using Finder.Properties;
using Finder.Utilities.KeyCapture;

namespace Finder.Views
{
    /// <summary>
    /// Interaction logic for Config.xaml
    /// </summary>
    public partial class Config : Window
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Config"/> class.
        /// </summary>
        public Config()
        {
            InitializeComponent();

            this.HotKeyCaptureBackground = this.btnHotKeyCapture.Background;

            ////if (Settings.Default.EmailToOutlook)
            ////{
            ////    RbEmailToOutlook.IsChecked = true;
            ////}
            ////else
            ////{
            ////    RbNameToOutlook.IsChecked = true;
            ////}
            
            btnHotKeyCapture.Content = Settings.Default.HotKey;

            //// KeyHooking.HotKeyIntercepted += this.KeyHookingHotKeyIntercepted;
        }

        /// <summary>
        /// Gets or sets the hot key capture background.
        /// </summary>
        /// <value>The hot key capture background.</value>
        protected Brush HotKeyCaptureBackground { get; set; }

        /// <summary>
        /// Gets or sets the recorded hot key.
        /// </summary>
        /// <value>The recorded hot key.</value>
        protected HotKey RecordedHotKey { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Config"/> is recording.
        /// </summary>
        /// <value><c>true</c> if recording; otherwise, <c>false</c>.</value>
        protected bool Recording { get; set; }

        /// <summary>
        /// Keys the hooking_ hot key intercepted.
        /// </summary>
        /// <param name="hotKey">The hot key.</param>
        private void KeyHookingHotKeyIntercepted(HotKey hotKey)
        {
            if (this.Recording)
            {
                if (hotKey.IsSet)
                {
                    btnHotKeyCapture.Content = hotKey.ToString();
                    this.RecordedHotKey = hotKey;
                    this.Recording = false;

                    this.SaveConfig();
                }
                else
                {
                    btnHotKeyCapture.Content = "No Hot Key Assigned!";
                }
            }

            btnHotKeyCapture.Background = this.HotKeyCaptureBackground;
        }

        /// <summary>
        /// Handles the Click event of the btnHotKeyCapture control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void RecordHotKey(object sender, RoutedEventArgs e)
        {
            this.Recording = true;
            this.btnHotKeyCapture.Content = "Press key combination";
            this.btnHotKeyCapture.Background = Brushes.Red;
        }

        /// <summary>
        /// Handles the Click event of the Button control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CloseConfig(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the 1 event of the Button_Click control.
        /// </summary>
        private void SaveConfig()
        {
            try
            {
                ////Settings.Default.EmailToOutlook = Convert.ToBoolean(RbEmailToOutlook.IsChecked);

                if (this.RecordedHotKey.IsSet)
                {
                    Settings.Default.HotKey = this.RecordedHotKey.ToString();
                    Settings.Default.HotKeyCode = this.RecordedHotKey.Key1.KeyCode + "|" + this.RecordedHotKey.Key2.KeyCode;
                }

                Settings.Default.Save();
            }
            catch
            {
                // Godzilla eat it!
            }
        }
    }
}